package cn.com.cifi.mars.util;

import java.io.IOException;
import java.nio.charset.Charset;

import com.alibaba.fastjson.JSONArray;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class BasicAuthClient {
	
	/**
	 * 构造Basic Auth认证头信息
	 * @return
	 */
	private String getHeader(String api_name,String api_key) {
		String auth = api_name + ":" + api_key;
		byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
		String authHeader = "Basic " + new String(encodedAuth);
		return authHeader;
	}

	public String post(String url,String jsonParam,String api_name,String api_key) {
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		StringEntity myEntity = new StringEntity(jsonParam, ContentType.APPLICATION_JSON); // 构造请求数据
		post.addHeader("Authorization", getHeader(api_name,api_key));
		post.setEntity(myEntity); // 设置请求体
		String responseContent = null; // 响应内容
		CloseableHttpResponse response = null;
		try {
			response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				responseContent = EntityUtils.toString(entity, "UTF-8");
			}
			if (response != null) {
				response.close();
			}
			if (client != null){
				client.close();
			}
		} catch (ClientProtocolException e) {
			log.error("###### errorMsg: ", e);
		} catch (IOException e) {
			log.error("###### errorMsg: ", e);
		}
		return responseContent;
	}
	
	
	public String get(String url, String filter,String api_name,String api_key) {
		
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet httpGet= new HttpGet(url+filter);
		httpGet.addHeader("Authorization", getHeader(api_name,api_key));
		 
		String responseContent = null; // 响应内容
		CloseableHttpResponse response = null;
		
		try {
			response = client.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				responseContent = EntityUtils.toString(entity, "UTF-8");
			}
			if (response != null){
				response.close();
			}
			if (client != null){
				client.close();
			}
		} catch (ClientProtocolException e) {
			log.error("###### errorMsg: ", e);
		} catch (IOException e) {
			log.error("###### errorMsg: ", e);
		}
		return responseContent;
	}
	
	
	public String getData(String url,String api_key,String api_value) {
		
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet httpGet= new HttpGet(url);
		httpGet.addHeader(api_key, api_value);
		 
		String responseContent = null; // 响应内容
		CloseableHttpResponse response = null;
		
		try {
			response = client.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				responseContent = EntityUtils.toString(entity, "UTF-8");
			}
			if (response != null){
				response.close();
			}
			if (client != null){
				client.close();
			}
		} catch (ClientProtocolException e) {
			log.error("###### errorMsg: ", e);
		} catch (IOException e) {
			log.error("###### errorMsg: ", e);
		}
		return responseContent;
	}
	
	public String postData(String url,String jsonParam,String api_key,String api_value,String token) {
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		StringEntity myEntity = new StringEntity(jsonParam, ContentType.APPLICATION_JSON); // 构造请求数据
		post.addHeader("Authorization", token);
		post.addHeader(api_key, api_value);
		post.setEntity(myEntity); // 设置请求体
		String responseContent = null; // 响应内容
		CloseableHttpResponse response = null;
		try {
			response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				responseContent = EntityUtils.toString(entity, "UTF-8");
			}
			if (response != null) {
				response.close();
			}
			if (client != null){
				client.close();
			}
		} catch (ClientProtocolException e) {
			log.error("###### errorMsg: ", e);
		} catch (IOException e) {
			log.error("###### errorMsg: ", e);
		}
		return responseContent;
	}
	
	/**
	 * 加密cookie
	 * @param cookie
	 * @return
	 */
	public static String encryptionCookie(String [] cookie) {
		byte[] cookies = new byte[cookie.length];
		for (int i = 0; i < cookie.length; i++) {
			 cookies[i] = (byte) Integer.parseInt(cookie[i]);
		}
		String msg=Base64.encodeBase64String(cookies);
	    return msg;
	}

	/**
	 * 加密
	 * @param cookie
	 * @return
	 */
	public static String encryptionArr(JSONArray arr){
		byte[] cookie =null;
		cookie = new byte[arr.size()];
		for (int i = 0;i < arr.size(); i++) {
			cookie[i] = (byte) arr.getIntValue(i);
		}
		//获取到cookie后进行转换
		String  resultcookie = Base64.encodeBase64String(cookie);
		return resultcookie;
	}
}
